﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace PhoneHelpers
{
    //Namespace: Microsoft.Phone.Info
    //Assembly:  Microsoft.Phone (in Microsoft.Phone.dll)
    public class DeviceIdentity
    {
            public System.String DeviceUniqueId 
            { 
                get 
                { 
                    return Convert.ToBase64String(GetDeviceProperty<byte[]>("DeviceUniqueId", false)); 
                } 
            }

            public System.String DeviceName 
            { 
                get 
                { 
                    return GetDeviceProperty<System.String>("DeviceName", false); 
                } 
            } 
            
            public System.String DeviceManufacturer 
            { 
                get 
                { 
                    return GetDeviceProperty<System.String>("DeviceManufacturer", false); 
                } 
            } 
            
            public System.String DeviceFirmwareVersion 
            { 
                get 
                { 
                    return GetDeviceProperty<System.String>("DeviceFirmwareVersion", false); 
                } 
            } 
            
            public System.String DeviceHardwareVersion 
            { 
                get 
                { 
                    return GetDeviceProperty<System.String>("DeviceHardwareVersion", false); 
                } 
            } 
            
            public System.Int64 DeviceTotalMemory 
            { 
                get 
                { 
                    return GetDeviceProperty<System.Int64>("DeviceTotalMemory", false); 
                } 
            } 
            
            public System.Int64 ApplicationCurrentMemoryUsage 
            { 
                get 
                { 
                    return GetDeviceProperty<System.Int64>("ApplicationCurrentMemoryUsage", false); 
                } 
            } 
            
            public System.Int64 ApplicationPeakMemoryUsage 
            { 
                get 
                { 
                    return GetDeviceProperty<System.Int64>("DeviceTotalMemory", false); 
                } 
            } 
            
            public System.Guid UserUniqueId 
            { 
                get 
                { 
                    return this.GetANID(); 
                } 
            } 
            
            private T GetDeviceProperty<T>(string name, bool isUserInfo) 
            {                
                object value;
                bool success = Microsoft.Phone.Info.DeviceExtendedProperties.TryGetValue(name, out value); 
                if (success) 
                    return (T)value; 
                return default(T); 
            } 
            
            private Guid GetANID() 
            { 
                object value;
                bool success = Microsoft.Phone.Info.DeviceExtendedProperties.TryGetValue("ANID", out value); 
                if (success) 
                { 
                    Match match = Regex.Match((string)value, "^A=(?<A>[0-9A-Fa-f]{8,8})(?<B>[0-9A-Fa-f]{4,4})(?<C>[0-9A-Fa-f]{4,4})(?<D>[0-9A-Fa-f]{4,4})(?<E>[0-9A-Fa-f]{12,12}).*$"); 
                    return new Guid(match.Groups["A"].Value + "-" + match.Groups["B"].Value + "-" + match.Groups["C"].Value + "-" + match.Groups["D"].Value + "-" + match.Groups["E"].Value); 
                };           
                return Guid.Empty; 
            } 
    }
}
